import datetime


def get_okex_futures_usdt_multiplier(base_currency, current_datetime=None):
  current_datetime = current_datetime if current_datetime is not None else \
                     datetime.datetime.utcnow()
  # https://okexsupport.zendesk.com/hc/en-us/articles/360035845611-I-OKEx-USDT-Margin-Futures-Introduction
  # https://okexsupport.zendesk.com/hc/en-us/articles/360040484491-Adjustment-of-Face-Value-for-USDT-margined-Perpetual-Swap-Futures-Trading
  if base_currency == 'BTC':
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1e-4
    else:
      multiplier = 1e-2
  elif base_currency == 'EOS':
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1e-1
    else:
      multiplier = 10
  elif base_currency == 'ETH':
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1e-3
    else:
      multiplier = 1e-1
  elif base_currency == 'LTC':
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1e-3
    else:
      multiplier = 1
  elif base_currency in ('BCHABC', 'BCHN'):
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1e-3
    else:
      multiplier = 1e-1
  elif base_currency == 'XRP':
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1
    else:
      multiplier = 100
  elif base_currency == 'ETC':
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1e-1
    else:
      multiplier = 10
  elif base_currency == 'BCHSV':
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1e-3
    else:
      multiplier = 1
  elif base_currency == 'TRX':
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1
    else:
      multiplier = 1000
  elif base_currency == 'LINK':
    multiplier = 1
  else:
    raise ValueError(base_currency)
  return multiplier

def get_huobi_swap_usdt_multiplier(base_currency, current_datetime=None):
  # https://support.hbfile.net/hc/en-us/articles/900003247446-Trading-Rules-of-USDT-Margined-Swaps
  return {
      'AAVE': 0.1,
      'ADA': 10,
      'ALGO': 10,
      'ATOM': 1,
      'AVAX': 1,
      'BAND': 1,
      'BCHABC': 0.01,
      'BCHN': 0.01,
      'BCHSV': 0.01,
      'BNB': 0.1,
      'BTC': 0.001,
      'COMP': 0.01,
      'CRV': 1,
      'DASH': 0.1,
      'DOT': 1,
      'DOGE': 100,
      'EOS': 1,
      'ETC': 1,
      'ETH': 0.01,
      'FIL': 0.1,
      'GRT': 10,
      'KAVA': 1,
      'KSM': 0.1,
      'LINK': 0.1,
      'LTC': 0.1,
      'NEO': 0.1,
      'OMG': 1,
      'ONT': 10,
      'RSR': 100,
      'SNX': 1,
      'SUSHI': 1,
      'THETA': 10,
      'TRX': 100,
      'UNI': 1,
      'VET': 100,
      'WAVES': 1,
      'XEM': 10,
      'XLM': 10,
      'XMR': 0.01,
      'XRP': 10,
      'XTZ': 1,
      'YFI': 0.0001,
      'YFII': 0.001,
      'ZEC': 0.1,
      'ZIL': 100,
  }[base_currency]

def get_okex_swap_usdt_multiplier(base_currency, current_datetime=None):
  # https://okexsupport.zendesk.com/hc/en-us/articles/360037503911-I-OKEx-USDT-Margined-Perpetual-Swap-Introduction
  # https://okexsupport.zendesk.com/hc/en-us/articles/360040484491-Adjustment-of-Face-Value-for-USDT-margined-Perpetual-Swap-Futures-Trading
  current_datetime = current_datetime if current_datetime is not None else \
                     datetime.datetime.utcnow()
  if base_currency == 'BTC':
    if current_datetime <= datetime.datetime(2020, 3, 20, 8):
      multiplier = 1e-4
    else:
      multiplier = 1e-2
  elif base_currency == 'EOS':
    if current_datetime <= datetime.datetime(2020, 3, 17, 8):
      multiplier = 1e-1
    else:
      multiplier = 10
  elif base_currency == 'ETH':
    if current_datetime <= datetime.datetime(2020, 3, 19, 8):
      multiplier = 1e-3
    else:
      multiplier = 1e-1
  elif base_currency == 'LTC':
    multiplier = 1
  elif base_currency in ('BCHABC', 'BCHN'):
    multiplier = 0.1
  elif base_currency == 'XRP':
    multiplier = 100
  elif base_currency == 'ETC':
    multiplier = 10
  elif base_currency == 'BCHSV':
    multiplier = 1
  elif base_currency == 'TRX':
    multiplier = 1000
  elif base_currency == 'ADA':
    multiplier = 100
  elif base_currency == 'ALGO':
    multiplier = 10
  elif base_currency == 'ATOM':
    multiplier = 1
  elif base_currency == 'DASH':
    multiplier = 0.1
  elif base_currency == 'IOST':
    multiplier = 1000
  elif base_currency == 'IOTA':
    multiplier = 10
  elif base_currency == 'KNC':
    multiplier = 1
  elif base_currency == 'LINK':
    multiplier = 1
  elif base_currency == 'NEO':
    multiplier = 1
  elif base_currency == 'ONT':
    multiplier = 10
  elif base_currency == 'QTUM':
    multiplier = 1
  elif base_currency == 'THETA':
    multiplier = 10
  elif base_currency == 'VSYS':
    multiplier = 100
  elif base_currency == 'XLM':
    multiplier = 100
  elif base_currency == 'XMR':
    multiplier = 0.1
  elif base_currency == 'XTZ':
    multiplier = 1
  elif base_currency == 'ZEC':
    multiplier = 0.1
  elif base_currency == 'DOT':
    multiplier = 1.0
  elif base_currency == 'FIL':
    multiplier = 0.1
  elif base_currency == 'YFI':
    multiplier = 0.0001
  elif base_currency == 'UNI':
    multiplier = 1
  elif base_currency == 'DOGE':
    multiplier = 1000
  else:
    raise ValueError(base_currency)
  return multiplier


def get_futures_multiplier(market_type, exchange, symbol, current_datetime=None):
  assert market_type == 'Futures'
  if exchange == 'Bitmex' and symbol.startswith('BTC-USD'):
    multiplier = 1
  elif exchange == 'Bitmex' and symbol == 'ETH-USD.PERPETUAL':
    multiplier = 1e-6
  elif exchange == 'Okex' and symbol.find('-USDT') >= 0:
    if symbol.endswith('.PERPETUAL'):
      multiplier = get_okex_swap_usdt_multiplier(symbol.split('-')[0], current_datetime)
    else:
      multiplier = get_okex_futures_usdt_multiplier(symbol.split('-')[0], current_datetime)
  elif exchange == 'Huobi' and symbol.find('-USDT') >= 0:
    if symbol.endswith('.PERPETUAL'):
      multiplier = get_huobi_swap_usdt_multiplier(symbol.split('-')[0], current_datetime)
    else:
      raise ValueError((exchange, symbol))
  elif exchange == 'Binance' and ((symbol.find('-USDT') >= 0) or (symbol.find('-BUSD') >= 0)):
    multiplier = 1
  elif exchange in ('Binance', 'Huobi', 'Okex'):
    multiplier = 100 if symbol.find('BTC') >= 0 else 10
  elif exchange in ('Bitflyer', 'Bybit', 'Ftx', 'Kraken', 'Prex'):
    multiplier = 1
  elif exchange == 'Deribit' and symbol.startswith('BTC-'):
    multiplier = 1
  elif exchange == 'Deribit' and symbol.startswith('ETH-'):
    multiplier = 1
  else:
    raise ValueError('Fail to get futures multiplier. %s %s %s' % (market_type, exchange, symbol))
  return multiplier

